package com.shop.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.entity.SupplierCouriers;
import com.shop.mapper.SupplierCouriersMapper;
import com.shop.mapper.SupplierWarehouseMapper;
import com.shop.model.PageParam;
import com.shop.model.vo.Rsp;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 骑手 yyt(SupplierCouriers)表服务接口
 *
 * @author makejava
 * @since 2020-11-21 10:12:52
 */
@Service
@AllArgsConstructor
public class SupplierCouriersService {
    private final SupplierCouriersMapper supplierCouriersMapper;
    private final SupplierWarehouseMapper supplierWarehouseMapper;
    /**
     * @author clh
     * @description 增
     * @date  2020/11/27 16:15
     * @param supplierCouriers
     * @param userId
     * @return {@link Rsp}
     **/
    public Rsp insertSelective(SupplierCouriers supplierCouriers,Integer userId) {
        if (supplierCouriersMapper.getPhone(supplierCouriers.getPhone()) > 0) {
            return Rsp.fail().msg("手机号码已存在");
        } else {
            supplierCouriers.setCreateUserId(userId);
            return Rsp.success(supplierCouriersMapper.insertSelective(supplierCouriers));
        }
    }
    /**
     * @author clh
     * @description 改
     * @date  2020/11/27 16:15
     * @param supplierCouriers
     * @param userId
     * @return {@link Rsp}
     **/
    public Rsp updateById(SupplierCouriers supplierCouriers,Integer userId){
        supplierCouriers.setUpdateUserId(userId);
        return Rsp.success(supplierCouriersMapper.updateById(supplierCouriers));
    }
    /**
     * @author clh
     * @description 删
     * @date  2020/11/27 16:16
     * @param supplierCouriers
     * @return {@link Rsp}
     **/
    public Rsp deleteById(SupplierCouriers supplierCouriers){

        return Rsp.success(supplierCouriersMapper.deleteById(supplierCouriers.getId()));
    }

    public PageInfo searchSomeByPage(PageParam<SupplierCouriers> pageParam){
        //开启分页
        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());

        //排序
        for (int i = 0; i <pageParam.getOrderParams().length ; i++) {
            PageHelper.orderBy(pageParam.getOrderParams()[i]);
        }
        List<SupplierCouriers> couriersList = supplierCouriersMapper.searchSomeByPage(pageParam.getSuperSearchKeyWord(), pageParam.getModel());
        for (SupplierCouriers couriers:couriersList) {
            couriers.setWarehouseName(supplierWarehouseMapper.selectById(couriers.getWarehouseId()).getName());
        }
        PageInfo<SupplierCouriers> pageInfo = new PageInfo<>(couriersList);
        return pageInfo;
    }
}
